package user;

import java.sql.*;

public class DisplayFoodPage
{
	//Display different list of food for different restaurant in the customer_page
	//Read a static variable from RestaurantQuery, not safe
	public static String getFoodInfo()
	{
		DBConnector db = DBConnector.getDBConnector();		
		String Rest_ID = RestaurantQuery.getRestaurantSelected();
		
		String statement = "select food_name, food_price, food_description, food_picture_link from food where Rest_ID = " + Rest_ID;
		db.prepareStatement(statement);
		ResultSet FoodInfo = db.queryPstmt();
		
		return displayFoodItems(FoodInfo);		
	}	
	
	//Display the searching result in the page search_result
	public static String displayFoodSearchingResult()
	{		
		ResultSet FoodInfo = FoodSearching.getFoodSearchingInfo();
		
		return displayFoodItems(FoodInfo);
	}
	
	//only called by functions getFoodInfo and displayFoodSearchingResult
	//the display format of the food information for both the search_result.jsp and the customer_page.jsp
	private static String displayFoodItems(ResultSet FoodInfo)
	{
		StringBuffer sBuf = new StringBuffer();
		try {	
			
			if (FoodInfo != null)
			{
				while (FoodInfo.next()){
		
					sBuf.append("<table width = 100% align = center border = 1><tr>");
					sBuf.append( "<td width=130><a name='" + FoodInfo.getString(1)+ "'>" + 
							"<img src='Pics/" + 
							FoodInfo.getString(4) + "' width=130 height=130 border = 0></a></td><td width=200>" +
							FoodInfo.getString(1) + " <br>" + 
							FoodInfo.getString(3) + "</td><td>Price : $" +
							FoodInfo.getString(2) + " </td>");	
					sBuf.append("<form method='post' action='servlet/FoodSelection' name = 'select_food'><div align='center'><td>" +
							"<input name='food_quantity' type='text' class='input' size='2' value='1'><br>");
					sBuf.append("<input name = 'food_selected' type = 'checkbox' class = 'input' value = '" + FoodInfo.getString(1) + "'><br>");
					sBuf.append("<input name='SelectButton' type='submit' value='Select'></td></div></form>");
					sBuf.append("</tr></table>");
				}
			}
			return sBuf.toString();
		}catch (SQLException ex){
			ex.printStackTrace();
			//System.out.println("The connection is not established");
			return "";
		}finally{
		}	
	}
}